package com.example.demo.demos.book.controller;

import com.example.demo.constant.Constants;
import com.example.demo.demos.book.enums.ResultCode;
import com.example.demo.model.*;
import com.example.demo.demos.book.service.BookService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.Arrays;
import java.util.List;


@Slf4j
@RestController
@RequestMapping("/book")
public class BookController {
    @Autowired
    private BookService bookService;

//    public BookController(){
//        bookService=new BookService();
//    }

//    @RequestMapping("/getBookList")
//    public List<BookInfo> getBookList() {
//        //1.获取图书的数据
//        //2.对图书数据进行修改，针对状态码数字（0：有货，1：没货这种）进行改变，把状态码数字转换成中文
//        //3.返回数据
//        List<BookInfo>bookInfos=bookService.getBookList();
//        return bookInfos;
//    }

    @RequestMapping("/getBookListByPage")
 public Result getBookListByPage(PageRequest pageRequest, HttpSession httpSession){
        log.info("查询翻页信息,pageRequest:{}",pageRequest);
        //用户登录校验
        UserInfo userInfo=(UserInfo) httpSession.getAttribute(Constants.SESSION_USER_KEY);
        if(userInfo==null||userInfo.getId()<=0||"".equals(userInfo.getUserName())){
            //用户未登录
           return Result.unlogin();
        }
        //校验成功
        if(pageRequest.getPageSize()<0||pageRequest.getCurrentPage()<1){
            return  Result.fail("参数校验失败");
        }
        PageResult<BookInfo> bookInfoPageResult = null;
        try{
            bookInfoPageResult=bookService.selectBookInfoByPage(pageRequest);
            return Result.success(bookInfoPageResult);
        }
            catch(Exception e){
                log.error("查询翻页信息错误,e{}",e);
                return Result.fail(e.getMessage());
        }
 }
 //设置后端的返回类型
 @RequestMapping(value = "/addBook",produces = "application/json")
 //或者通过前端，把字符串转化为对象(因为前端没有字符串) JSON.parse(result)
    public String addBoook(BookInfo bookInfo){
        log.info("接收到添加图书请求,bookInfo:{}",bookInfo);
        //参数校验
            if(!StringUtils.hasLength(bookInfo.getBookName())
             ||!StringUtils.hasLength(bookInfo.getAuthor())
             ||bookInfo.getCount()<0
                    ||bookInfo.getPrice()==null
             ||!StringUtils.hasLength(bookInfo.getPublish())){
         return "参数校验失败，请检查参数";
     }
     Integer result=bookService.addBook(bookInfo);
     if(result<=0){
         log.error("添加出现错误:bookInfo:{}",bookInfo);
         return "添加图书出错,请联系管理员";
     }
     return  "";
 }
 @RequestMapping("/queryBookInfoById")
 public BookInfo queryBookInfoById(Integer bookId){
        log.info("根据ID查询图书，bookId"+bookId);
     BookInfo bookInfo=null;
        try{
            bookInfo=bookService.queryBookInfoById(bookId);
            return  bookInfo;
        }
        catch (Exception e){
            log.error("查询图书失败.e{}",e);
        }
      return null;
 }
 @RequestMapping("/updateBook")
 public  String updateBook(BookInfo bookInfo){
        log.info("接收到更新图书的请求，bookInfo{}",bookInfo);
       Integer result=bookService.updateBook(bookInfo);
       if(result==0){
           log.error("更新图书失败，请联系管理员");
           return "更新图书失败，请联系管理员";
       }
       return " ";
 }
 @RequestMapping("/batchDelete")
 //也可以使用list接收
 //那样参数就会变成@RequestParam List<Integer> ids,然后调用是直接就ids
 public String batchDelete(@RequestParam List<Integer> ids){
        log.info("接收请求，批量删除图书，图书ID：{}",ids);
        Integer result=bookService.batchDelete(ids);
        if (result<=0){
            log.error("批量删除失败,请联系管理员");
            return "批量删除失败，请联系管理员";
        }
        return "";
 }

}